package com.tangjia.wangyi;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    public static final String TAG ="test";
    ArrayList<NewsList> newsLists;
    String urlString;
    NewsAdapter newsAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loadData();
        newsAdapter = new NewsAdapter(newsLists);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
        RecyclerView recyclerView =(RecyclerView) findViewById(R.id.list);
        recyclerView.setLayoutManager(linearLayoutManager);

        HttpUtil.sendOkHttpRequest("http://c.m.163.com/nc/article/headline/T1348647853363/0-40.html", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i(TAG, "onFailure: ========================================");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String jsonData=response.body().string();
                //Log.i(TAG, "onFailure: ="+);
                try {
                    Log.i(TAG, jsonData );
                    JSONObject json = new JSONObject(jsonData);
                    JSONArray t1348647853363 = json.getJSONArray("T1348647853363");

                    for (int i = 0; i < t1348647853363.length(); i++) {
                        JSONObject jsonObject = t1348647853363.getJSONObject(i);
                        newsLists.add(new NewsList(jsonObject.getString("ltitle"),jsonObject.getString("url")));
                        Log.i(TAG, "=="+jsonObject.getString("ltitle")+jsonObject.getString("url"));

                    }
                    newsAdapter.notifyDataSetChanged();
                   // Log.i(TAG, "name is " );
                } catch (Exception e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        recyclerView.setAdapter(new NewsAdapter(newsLists));
                    }
                });

            }
        });



    }

    void loadData(){
        newsLists = new ArrayList<>();
        /*newsLists.add(new NewsList("123"));
        newsLists.add(new NewsList("123"));
        newsLists.add(new NewsList("123"));
        newsLists.add(new NewsList("123"));*/

    }


}